package asm.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class TargetProxy implements MethodInterceptor {
    public <T> T getProxy(Class clazz) {
        // 字节码增强的一个类
        Enhancer enhancer = new Enhancer();
        // 设置父类
        enhancer.setSuperclass(clazz);
        // 设置回调类
        enhancer.setCallback(this);
        // 创建代理类
        return (T) enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println(method.getName() + "数据缓存...");
        return methodProxy.invokeSuper(o, objects);
    }
}
